package com.gc.ykqx.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.gc.ykqx.R;
import com.gc.ykqx.service.entity.Count;
import com.gc.ykqx.service.entity.Site;
import com.gc.ykqx.service.util.Constant;
import com.gc.ykqx.service.util.ParseUtil;
import com.gc.ykqx.ui.adapter.CountListAdapter;
import com.gc.ykqx.ui.custom.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

// TODO: 2017/10/12  I will improve this class
public class CountActivity extends BaseActivity implements CountListAdapter.OnCheckedChangeListener {

    @BindView(R.id.btn_back)
    ImageButton btnBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_count)
    ListView listView;
    @BindView(R.id.tv_edit)
    TextView tvEdit;
    private Unbinder unbinder;
    private static final String TAG = "CountActivity";
    private CountListAdapter adapter;
    private List<Count> countList;
    private ArrayList<Site> siteList;
    private String title;
    private String siteType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_count);
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        tvEdit.setText("编辑");
        Intent intent = getIntent();
        title = intent.getStringExtra(Constant.TransmissionKey.TITLE);
        siteList = intent.getParcelableArrayListExtra(Constant.TransmissionKey.SITE_LIST);
        siteType = intent.getStringExtra(Constant.TransmissionKey.SITE_TYPE);
        if (siteList != null) {
            countList = ParseUtil.getCountList(siteList);
            adapter = new CountListAdapter(this, countList, siteType);
            listView.setAdapter(adapter);
            adapter.setListener(CountActivity.this);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_back, R.id.tv_edit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.tv_edit:
                if (tvEdit.getText().equals("编辑")) {
                    for (int i = 7; i < countList.size(); i++) {
                        Count count = countList.get(i);
                        count.setShow(true);
                        count.setChecked(false);
                    }
                    adapter.notifyDataSetChanged();
                    tvEdit.setText("删除");
                } else {
                    for (int i = 7; i < countList.size(); i++) {
                        Count count = countList.get(i);
                        count.setShow(false);
                        if (count.isChecked()) {
                            countList.remove(i);
                            i--;
                        }
                    }
                    ParseUtil.parseCountList(countList, siteType);
                    adapter.notifyDataSetChanged();
                    tvEdit.setText("编辑");
                }
                break;
        }
    }

    @Override
    public void onChecked(int position) {
        Log.e(TAG, "onChecked: " + position);
        Count count = countList.get(position);
        if (count.isChecked()) {
            count.setChecked(false);
        } else {
            count.setChecked(true);
        }

    }
}
